package com.foreknow.tijian.service.Impl;

import com.foreknow.tijian.dao.CartDao;
import com.foreknow.tijian.dao.Impl.CartDaoImpl;
import com.foreknow.tijian.po.Cart;
import com.foreknow.tijian.service.CartService;
import com.foreknow.tijian.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

public class CartServiceImpl implements CartService {
    Connection conn= DBUtil.getConnection();
    PreparedStatement pst;
    @Override
    public List<Cart> listCart(Cart cart) {
        List<Cart> cartList = null;
        CartDao cartDao = new CartDaoImpl();
        try {
            cartList = cartDao.listCart(cart);
        } finally {
            DBUtil.close();
        }
        return cartList;
    }



    @Override
    public int saveCart(String userId, int businessId, int foodId) {
        int result = 0;
        CartDao cartDao = new CartDaoImpl();
        try {
            //开始事务
            DBUtil.beginTransaction();
            result = cartDao.saveCart(userId, businessId, foodId);
            //提交事务
            DBUtil.commitTransaction();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close();
        }
        return result;
    }

    @Override
    public int updateCart(Cart cart) {
       int result = 0;
        CartDao cartDao = new CartDaoImpl();
        try{
            DBUtil.beginTransaction();
            result = cartDao.updateCart(cart);
            //提交事务
            DBUtil.commitTransaction();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            DBUtil.close();
        }
        return result;
    }

    @Override
    public int removeCart(Cart cart) {
        int res=0;
        CartDao cartDao = new CartDaoImpl();
        try{
            DBUtil.beginTransaction();
            res=cartDao.removeCart(cart);
            //提交事务
            DBUtil.commitTransaction();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        finally {
            DBUtil.close();
        }
        return res;
    }

}
